package com.ts.api.module.channel.service.impl;

import com.gitee.apanlh.util.base.CollUtils;
import com.gitee.apanlh.util.base.Eq;
import com.gitee.apanlh.util.valid.ValidParam;
import com.ts.api.common.context.ChannelContext;
import com.ts.api.common.context.ChannelRequestContext;
import com.ts.api.common.localcache.DistrictCache;
import com.ts.api.common.util.OpenTimeUtils;
import com.ts.api.module.channel.dao.ChannelMarketDao;
import com.ts.api.module.channel.entity.po.ChannelMarketPO;
import com.ts.api.module.channel.service.ChannelMarketService;
import com.ts.api.module.common.entity.DistrictPO;
import com.ts.api.module.common.entity.bo.IpInfoBO;
import com.ts.api.module.common.service.IpInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ChannelMarketServiceImpl implements ChannelMarketService {

    @Autowired
    private ChannelMarketDao channelMarketDao;
    @Autowired
    private IpInfoService ipInfoService;

    @Override
    @Transactional(readOnly = true)
    public List<ChannelMarketPO> list(boolean enableFilter) {
        Long channelId = ChannelContext.getId();
        List<ChannelMarketPO> list = channelMarketDao.list(channelId);

        //  不存在或不开启过滤则直接返回原始对象
        if (ValidParam.isEmpty(list) || !enableFilter) {
            return list;
        }

        //  根据当前贷超-获取存在城市的贷超列表
        List<Long> existMarketArea = channelMarketDao.listMarketAreaByMarketId(CollUtils.toList(list, t -> t.getId()));
        if (ValidParam.isNotEmpty(existMarketArea)) {
            //  获取ip地区
            String ip = ChannelRequestContext.get().getIp();
            IpInfoBO ipInfoBO = ipInfoService.getByIp(ip);
            String cityName = ipInfoBO.getCity();
            DistrictPO districtPO = DistrictCache.getByName(cityName);

            //  过滤地区
            if (ValidParam.isNotNull(districtPO)) {
                List<Long> listMarketIds = channelMarketDao.listMarketAreaByAreaCode(districtPO.getCode());
                CollUtils.filter(list, t -> {
                    Long findId = CollUtils.findOne(listMarketIds, marketId -> Eq.object(t.getId(), marketId));
                    return ValidParam.isNotNull(findId);
                });
            }
        }

        //  过滤未开放时间的贷超
        List<ChannelMarketPO> filterTimeMarket = OpenTimeUtils.filter(list);
        return filterTimeMarket;
    }

    @Override
    @Transactional(readOnly = true)
    public ChannelMarketPO get(Long channelId, Long marketId) {
        return channelMarketDao.get(channelId, marketId);
    }

    @Override
    @Transactional(readOnly = true)
    public int countByChannelIdAndMarketId(Long channelId, Long marketId) {
        return channelMarketDao.countByChannelIdAndMarketId(channelId, marketId);
    }

    @Override
    @Transactional(readOnly = true)
    public List<Long> listMarketAreaByAreaCode(String areaCode) {
        return channelMarketDao.listMarketAreaByAreaCode(areaCode);
    }
}
